package algorithm.leetcode;

public class Num206 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode head_1 = new ListNode(2);
        ListNode head_2 = new ListNode(3);
        ListNode head_3 = new ListNode(4);
        ListNode head_4 = new ListNode(5);
        ListNode head_5 = new ListNode(6);
        head.next = head_1;
        head_1.next = head_2;
        head_2.next = head_3;
        head_3.next = head_4;
        head_4.next = head_5;
        System.out.println(head);
        System.out.println(reverseList(head));
    }

    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        return pre;
    }
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }
}
